/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.NewsEvent;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


/**
 *
 * @author Young Ran
 */
public class NewsEventDAO {
    
    private Connection cn;
    private PreparedStatement pstm;
    private CallableStatement cs;
    
    
    
    public NewsEventDAO() {
        cn = DB_Util.getConnection();
    }
    
    private ArrayList getNewsEventData(ResultSet rs) throws SQLException {
        ArrayList list = new ArrayList();
        while (rs.next()) {
            NewsEvent e = new NewsEvent();
            e.setId(rs.getInt("NewsEventId"));
            e.setName(rs.getString("NewsEventName"));
            e.setDetail(rs.getString("NewsEventDetail"));
            e.setType(rs.getString("NewsEventType"));
            e.setTime(rs.getString("NewsEventTime"));
            try {
                e.setImage(ImageIO.read(rs.getBinaryStream("NewsEventPicture")));
            } catch (IOException ex) {
                e.setImage(null);
            }
            list.add(e);
        }
        return list;
    }
    
    
    //DISPLAY NEWSEVENTS:
    public ArrayList getAllNewsEvent() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_NewsEvent}");
        ResultSet rs = cs.executeQuery();
        return getNewsEventData(rs);
    }
    public void addNewsEvent(NewsEvent item) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Add_NewsEvent(?,?,?,?,?)}");
        cs.setNString(1, item.getName());
        cs.setNString(2, item.getDetail());
        cs.setNString(3, item.getType());
        cs.setString(4,item.getTime());
        ByteArrayOutputStream picOut = new ByteArrayOutputStream();
        try {
            ImageIO.write((RenderedImage) item.getImage(), "jpg", picOut);
        } catch (IOException ex) {
            Logger.getLogger(NewsEventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        cs.setBytes(5, picOut.toByteArray());
        cs.execute();
    }
    public void editNewsEvent(NewsEvent item) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Edit_NewsEvent(?,?,?,?,?,?)}");
        cs.setInt(1, item.getId());
        cs.setNString(2, item.getName());
        cs.setNString(3, item.getDetail());
        cs.setNString(4, item.getType());
        cs.setString(5,item.getTime());
        if(item.getImage()!=null)
        {
        ByteArrayOutputStream picOut = new ByteArrayOutputStream();
        try {
            ImageIO.write((RenderedImage) item.getImage(), "jpg", picOut);
        } catch (IOException ex) {
            Logger.getLogger(NewsEventDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        cs.setBytes(6, picOut.toByteArray());
        }
        else cs.setBytes(6, null);
        cs.execute();
    }
    public void deleteNewsEvent(int id) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Delete_NewsEvent(?)}");
        cs.setInt(1, id);
        cs.execute();
    
    }
    
}
